#include "OPA_CMP_679x.h"

void    initOPA0(void)
{
    uint8 i;
    uint8 tmp;
    uint8 opaout;

    SEL_CMP0S_RA2();

    //-------- OPA0 校准 --------//
    OPA0_EN();
    OPA0_SEL_CALI();
    OPA0_CALI_IN_N();

    OPA0ADJ &= 0b11110000;
    tmp = OPA0ADJ;
    _delay(100);

    opaout = OPA0ADJ & 0b10000000;
    for(i=0; i<16; i++)
    {
        OPA0ADJ = tmp;
        _delay(100);
        if(opaout != (OPA0ADJ&0b10000000))
        {
            break;
        }
        tmp++;
    }
    OPA0_SEL_NORMAL();

    //-------- OPA0 配置 --------//
    OPA0_FT_DIS();
    OPA0_OEN();
    OPA0_NCNT_ADC();
    OPA0_SEL_CMP();
    // SET_CMP0REF(CMP0REF_1_10VDD);
    SET_CMP0REF(CMP0REF_3_10VDD);
    OPA0_OEN();

}

void    initOPA1(void)
{

}

void    SET_CMP0REF(uint8 sel)
{
    uint8 tmp;

    tmp = CMPCON;
    tmp &= ~0b00000111;
    tmp |= sel;

    CMPCON = tmp;
}

void    SET_CMP1REF(uint8 sel)
{
    uint8 tmp;

    tmp = CMPCON;
    tmp &= ~0b01110000;
    tmp |= sel;

    CMPCON = tmp;
}

